package com.litchi.tutor.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.litchi.tutor.common.cons.RedisKey;
import com.litchi.tutor.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author hoby
 * @since 2021-08-20
 */
@Slf4j
public class WxUtil {

    /**
     * 获取用户信息
     * @param access_token
     * @param openId
     * @return
     */
    public static String getUserInfo(String access_token, String openId) {
        String url2 = "https://api.weixin.qq.com/sns/userinfo?access_token={}&openid={}&lang=zh_CN";
        url2 = StrUtil.format(url2, access_token, openId);
        String json = HttpUtil.get(url2);
        return json;
    }

    /**
     * 获取access_token openId
     * @param code
     * @param appId
     * @param appSecret
     * @return
     */
    public static String getAccessTokenAndOpenId(String code, String appId, String appSecret) {
        String url1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={}&secret={}&code={}&grant_type=authorization_code";
        url1 = StrUtil.format(url1, appId, appSecret, code);
        String accesstoken = HttpUtil.get(url1);
        JSONObject jsonObject = JSONObject.parseObject(accesstoken);
        return String.valueOf(jsonObject);
    }


    // ------------------------- 以下为之前的 -------------------------

    /**
     * 获取用户信息
     * @param code
     * @return
     */
//    public static JSONObject getUserInfo(String code, String appId, String appSecret) {
//        String accessToken = getAccessToken(appId, appSecret);
//        String openid = getOpenid(code, appId, appSecret);
////        getAccessTokenAndOpenId(code, )
//        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={}&openid={}&lang=zh_CN";
//        url = StrUtil.format(url, accessToken, openid);
//        String json = HttpUtil.get(url);
//        JSONObject obj = JSON.parseObject(json);
//        System.out.println("--------------wxUtil--------------");
//        System.out.println(json);
//        System.out.println("--------------wxUtil--------------");
//
//        if (obj.containsKey("errcode")) {
//            throw new CustomException(obj.getInteger("errcode"), obj.getString("errmsg"));
//        }
//        return obj;
//    }

    /**
     * 获取用户openid
     * @param code
     * @param appId
     * @param appSecret
     * @return
     */
//    public static String getOpenid(String code, String appId, String appSecret) {
//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?code={}&appid={}&secret={}&grant_type=authorization_code";
//        url = StrUtil.format(url, code, appId, appSecret);
//        String json = HttpUtil.get(url);
//        JSONObject obj = JSON.parseObject(json);
//        if (obj.containsKey("errcode")) {
//            throw new CustomException(obj.getInteger("errcode"), obj.getString("errmsg"));
//        }
//        return obj.getString("openid");
//    }

    /**
     * 获取普通access_token
     * @param appId
     * @param appSecret
     * @return
     */
//    public static String getAccessToken(String appId, String appSecret) {
//        RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
//        String cacheToken = redisUtil.get(RedisKey.WEIXIN_ACCESS_TOKEN);
//        if (StrUtil.isNotEmpty(cacheToken)) {
//            return cacheToken;
//        }
//
//        String url = "https://api.weixin.qq.com/cgi-bin/token?&appid={}&secret={}&grant_type=client_credential";
//        url = StrUtil.format(url, appId, appSecret);
//        String json = HttpUtil.get(url);
//        JSONObject obj = JSON.parseObject(json);
//        if (obj.containsKey("errcode")) {
//            throw new CustomException(obj.getInteger("errcode"), obj.getString("errmsg"));
//        }
//        String token = obj.getString("access_token");
//
//        redisUtil.set(RedisKey.WEIXIN_ACCESS_TOKEN, token, 1, TimeUnit.HOURS);
//        return token;
//    }
}
